package com.example.testjenkins.config;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

//@Component
public class TokenManager {

    private long tokenLift = 60 * 1000L;

    private static String key = "12345";

    public static String createToken(String username) {
        String token = Jwts.builder().setSubject(username)
                .setIssuedAt(new Date())
                .setIssuer("jwt签发人")          // issuer：jwt签发人
//                .setExpiration(new Date(System.currentTimeMillis() + tokenLift))
                .signWith(SignatureAlgorithm.HS512, key).compressWith(CompressionCodecs.GZIP).compact();
        return token;
    }

    public static Map parseToken(String token) {
        Map map = (Map) Jwts.parser().setSigningKey(key).parse(token).getBody();
        return map;
    }

    public static void main(String[] args) {
        String token = createToken("admin");
        System.out.println(token);
        Map map = parseToken(token);
        map.forEach((k, v) -> {
            System.out.println(k + "..." + v);
        });
    }
}
